package com.xxl.job.executor.synchronizeData.handleOne.dataProcessing.executor;

import cn.hutool.core.thread.ThreadUtil;
import com.xxl.job.executor.synchronizeData.handleOne.dataReception.bean.TblBaseScore;
import com.xxl.job.executor.synchronizeData.handleOne.dataReception.service.TblBaseScoreService;
import com.xxl.job.executor.synchronizeData.handleOne.dataSources.bean.TblBaseScoreSources;
import com.xxl.job.executor.synchronizeData.handleOne.dataSources.service.TblBaseScoreSourcesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;

/**
 * @author hanyq
 * @date 2023/11/1 17:48
 */
@Slf4j
@Component
public class synDataHandleOneExecutor {

    @Resource
    private TblBaseScoreSourcesService tblBaseScoreSourcesService;

    @Resource
    private TblBaseScoreService tblBaseScoreService;

    public void handler(Boolean full) {
        try {
            //这里模拟全量
            List<TblBaseScoreSources> list = tblBaseScoreSourcesService.list();
            for (TblBaseScoreSources tblBaseScoreSources : list){
                TblBaseScore byId = tblBaseScoreService.getById(tblBaseScoreSources.getId());
                byId = copyProperties(byId,tblBaseScoreSources);
                if (byId != null) {
                    tblBaseScoreService.saveOrUpdate(byId);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("同步完成喽");
        }

        ExecutorService executorService = ThreadUtil.newExecutor(2);
        CountDownLatch countDownLatch = ThreadUtil.newCountDownLatch(2);
        executorService.execute(() -> {
            try {
                //多线程可以在这里进行操作
                System.out.println("我是多线程操作啦啦啦啦====我好像没干什么但是我很重要11111111111111");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                countDownLatch.countDown();
            }
        });

        executorService.execute(() -> {
            try {
                //多线程可以在这里进行操作
                System.out.println("我是多线程操作啦啦啦啦====我好像没干什么但是我很重要22222222222222");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                countDownLatch.countDown();
            }
        });

        //等待所有线程执行完毕后先关闭线程池
        try {
            countDownLatch.await();
            executorService.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private TblBaseScore copyProperties(TblBaseScore tblBaseScore,TblBaseScoreSources busi) {
        try{
            if (tblBaseScore == null){
                tblBaseScore = new TblBaseScore();
            }
            tblBaseScore.setId(busi.getId());
            tblBaseScore.setName(busi.getName());
            tblBaseScore.setScore(busi.getScore());
            tblBaseScore.setAddress(busi.getAddress());
            tblBaseScore.setPhone(busi.getTel());
            tblBaseScore.setSyncTime(busi.getSyncTime());
            return tblBaseScore;
        }catch (Exception e){
            e.printStackTrace();
        }
        return tblBaseScore;
    }
}
